<?php

namespace app\api\controller;

use think\Db;
use app\common\validate\CartValidate;

class Shopcart extends ApiBase
{
	
	protected $datas;

	/**
	 * 加入购物车
	 * @Author   Yichen
	 * @DateTime 2020-05-04T17:43:55+0800
	 */
	public function addCart()
	{
		$this->datas = $this->params;
		$validate = new CartValidate();
		if (!$validate->scene('add')->check($this->datas)) {
			return json(['code' => 0, 'msg' => $validate->getError(), 'data' => '']);
		}
		$userId = session('userinfo.uid');
		$goodsProId = $this->datas['goodsProId'];
		$number = $this->datas['number'];
		$isExist = Db::name('t_shopcart')->where(['user_id' => $userId, 'goods_property_id' => $goodsProId])->find();
		if (!empty($isExist)) {
			$total = $isExist['number'] + $number;
			$result = Db::name('t_shopcart')
				->where(['user_id' => $userId, 'goods_property_id' => $goodsProId])
				->update([
					'number' => $total, 
					'update_time' => time()
				]);
		} else {
			$data = [
				'user_id' => $userId,
				'goods_property_id' => $goodsProId,
				'number' => $number,
				'create_time' => time()
			];
			$result = Db::name('t_shopcart')->insert($data);
		}
		if (!$result) {
			return json(['code' => 0, 'msg' => '添加失败', 'data' => '']);
		}
		return json(['code' => 1, 'msg' => '添加成功', 'data' => '']);
	}

	/**
	 * 我的购物车列表
	 * @Author   Yichen
	 * @DateTime 2020-05-10T17:21:39+0800
	 * @return   [type]                   [description]
	 */
	public function myShopcart()
	{
		$uid = session('userinfo.uid');
		$data = Db::name('t_shopcart')->alias('s')
			->join('t_goods_property gp', 's.goods_property_id=gp.id', 'left')
			->join('t_goods g', 'g.id=gp.goods_id', 'left')
			->where('user_id', $uid)
			->field('s.id,g.name,gp.specifications_name speName,gp.price,gp.big_images image,s.number,FROM_UNIXTIME(s.create_time,"%Y-%m-%d %H:%i:%S") createTime')
			->select();
		return json(['code' => 1, 'msg' => '', 'data' => $data]);	
	}

	/**
	 * 删除购物车内商品
	 * @Author   Yichen
	 * @DateTime 2020-05-13T22:31:24+0800
	 * @return   [type]                   [description]
	 */
	public function delCartGoods()
	{
		$this->datas = $this->params;
		$validate = new CartValidate();
		if (!$validate->scene('del')->check($this->datas)) {
			return json(['code' => 0, 'msg' => $validate->getError(), 'data' => '']);
		}
		$id = $this->datas['id'];
		$result = Db::name('t_shopcart')->where('id', $id)->delete();
		if (!$result) {
			return json(['code' => 0, 'msg' => '删除失败', 'data' => '']);
		}
		return json(['code' => 1, 'msg' => '删除成功', 'data' => '']);
	}

	/**
	 * 购物车内商品加或减
	 * @Author   Yichen
	 * @DateTime 2020-05-13T23:03:42+0800
	 * @return   [type]                   [description]
	 */
	public function putQtyAndReturn()
	{
		$this->datas = $this->params;
		$validate = new CartValidate();
		if (!$validate->scene('putQty')->check($this->datas)) {
			return json(['code' => 0, 'msg' => $validate->getError(), 'data' => '']);
		}
		$id = $this->datas['id'];
		$qty = $this->datas['number'];
		$number = Db::name('t_shopcart')->where('id', $id)->value('number');
		if ($number == 0) {
			return json(['code' => 0, 'msg' => '异常', 'data' => '']);
		}
		$result = Db::name('t_shopcart')
			->where('id', $id)
			->update(['number' => $qty, 'update_time' => time()]);
		if (!$result) {
			return json(['code' => 0, 'msg' => '失败', 'data' => '']);
		}	
		return json(['code' => 1, 'msg' => '成功', 'data' => '']);
	}
}